package com.helloblog.dao.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.helloblog.dao.UserDao;
import com.helloblog.domain.User;
import com.helloblog.exception.UserDaoException;

import static com.helloblog.utils.DateSourceUtils.*;

public class UserDaoImpl implements UserDao {
	private List<User> list = new ArrayList<User>();
	
	public void addUser(User user){	
		try {
			QueryRunner qr = new QueryRunner(getDataSource());
			String sql = "insert into user(id,username,password,gender,cellphone,email,registetime,lastlogintime,lastloginip,is_openblog) values(?,?,?,?,?,?,?,?,?,?) ";
			Object param[] = {user.getId(),user.getUsername(),user.getPassword(),user.getGender(),user.getCellphone(),user.getEmail()
				,user.getRegistetime(),	user.getLastlogintime(),user.getLastloginip(),user.getIs_openblog()
			};
			qr.update(sql, param);
		} catch (Exception e) {
			throw new UserDaoException(e);
		}
		
	}
	public void deleteUser(String id){
		try {
			QueryRunner qr = new QueryRunner(getDataSource());
			String sql = "delete from user where id='"+id+"'";
			qr.update(sql);
		} catch (Exception e) {
			throw new UserDaoException(e);
		}
	}
	public void updateUser(User user){
		try {
			QueryRunner qr = new QueryRunner(getDataSource());
			String sql = "update user set gender=?,cellphone=?,email=? where id=?";
			Object params[] = {user.getGender(),user.getCellphone(),user.getEmail(),user.getId()};
			qr.update(sql, params);
		} catch (Exception e) {
			throw new UserDaoException(e);
		}		
	}
	public User findUser(String username){
		try {
			QueryRunner qr = new QueryRunner(getDataSource());
			String sql = "select *from user where username='"+username+"'";
			return qr.query(sql,new BeanHandler<User>(User.class));
		} catch (Exception e) {
			throw new UserDaoException(e);
		}
	}
	
	public User findUserByID(String userid){
		try {
			QueryRunner qr = new QueryRunner(getDataSource());
			String sql = "select *from user where id='"+userid+"'";
			return qr.query(sql,new BeanHandler<User>(User.class));
		} catch (Exception e) {
			throw new UserDaoException(e);
		}
	}
	public void modifyUserPassword(User user){
		try {
			QueryRunner qr = new QueryRunner(getDataSource());
			String sql = "update user set password=? where username=?";
			Object params[] = {user.getPassword(),user.getUsername()};
			qr.update(sql, params);
		} catch (Exception e) {
			throw new UserDaoException(e);
		}
	}
	
	public User loginUser(String username,String password){
		try {
			QueryRunner qr = new QueryRunner(getDataSource());
			String sql = "select *from user where username=? and password=?";
			Object params[] = {username,password};
			return qr.query(sql, new BeanHandler<User>(User.class), params);
		} catch (Exception e) {
			throw new UserDaoException(e);
		}	
	}
	
	public void updateLastLoginInfo(String id,Date datetime,String ip ){
		try {
			QueryRunner qr = new QueryRunner(getDataSource());
			String sql = "update user set lastlogintime=?,lastloginip=? where id=?";
			Object params[] = {datetime,ip,id};
			qr.update(sql, params);
		} catch (Exception e) {
			throw new UserDaoException(e);
		}	
	}
	public void updateWithOpenBlog(String id){
		try {
			QueryRunner qr = new QueryRunner(getDataSource());
			String sql = "update user set is_openblog=1 where id=?";
			Object param = id;
			qr.update(sql, param);
		} catch (Exception e) {
			
			throw new UserDaoException(e);
		}	
	}
}
